﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;

namespace PSI
{
    public partial class Rejestracja : System.Web.UI.Page
    {
        ConnectionStringSettings settings;
        string connectionString;
        LINQDataContext bd = new LINQDataContext();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            settings = System.Configuration.ConfigurationManager.ConnectionStrings["PSIConnectionString"];
            connectionString = settings.ConnectionString;

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            psi_danefirmy nowe = new psi_danefirmy();


            var query = from c in bd.psi_danefirmies
                        where (c.login == TextBox4.Text && c.haslo == TextBox5.Text)
                        select c;

            string login="";
            string ID = "";
            foreach (var q in query)
            {
                login = q.login;
                ID = q.firma_id.ToString();
            }

            if (login != "")
            {
                FormsAuthentication.Initialize();
                FormsAuthenticationTicket bilet = new FormsAuthenticationTicket(1, TextBox4.Text, DateTime.Now, DateTime.Now.AddMinutes(15), false, FormsAuthentication.FormsCookiePath);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(bilet));
                Response.Cookies.Add(cookie);


                HttpCookie myCookie = new HttpCookie("UserSettings");
                myCookie["idFirmy"] = ID;
                myCookie["login"] = login;
                myCookie.Expires = DateTime.Now.AddMinutes(60);
                Response.Cookies.Add(myCookie);

                Server.Transfer("start.aspx");
            }
            else
            {
                Label4.Visible = true;
            }

            //using (SqlConnection connection = new SqlConnection(connectionString))
            //{

            //    connection.Open();
            //    using (SqlCommand command =
            //          new SqlCommand("select * from dbo.psi_danefirmy where login = '" + TextBox4.Text + "' and haslo= '" + TextBox5.Text + "'", connection))
            //    {

            //        SqlDataReader wynik = command.ExecuteReader();
            //        wynik.Read();
            //        if (wynik.HasRows)
            //        {
            //            FormsAuthentication.Initialize();
            //            FormsAuthenticationTicket bilet = new FormsAuthenticationTicket(1, TextBox4.Text, DateTime.Now, DateTime.Now.AddMinutes(15), false, FormsAuthentication.FormsCookiePath);
            //            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(bilet));
            //            Response.Cookies.Add(cookie);

                        
            //                    HttpCookie myCookie = new HttpCookie("UserSettings");
            //                    myCookie["id"] = wynik.GetValue(0).ToString();
            //                    myCookie["login"] = wynik.GetValue(6).ToString();
            //                    myCookie.Expires = DateTime.Now.AddMinutes(60);
            //                    Response.Cookies.Add(myCookie);
            //                    //Label6.Text = wynik.GetValue(0).ToString();
            //                    //Label7.Text = wynik.GetValue(6).ToString();

            //                    Server.Transfer("start.aspx");

                           
            //        }
            //        else Label4.Visible = true;
            //    }
            //}
        }

        public void Czysc()
        {
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Czysc();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            psi_danefirmy dane = new psi_danefirmy();
            string data = Label1.Text; //12-11-2011 
            dane.nazwa = TextBox6.Text;
            dane.login = TextBox7.Text;
            dane.haslo = TextBox8.Text;
            
            bd.psi_danefirmies.InsertOnSubmit(dane);
            bd.SubmitChanges(); 

            Czysc();
            Label10.ForeColor = System.Drawing.Color.Green;
            Label10.Text = "Zarejestrowano nowego uzytkownika";
           // Label10.Text = "insert into dbo.psi_danefirmy values ('" + TextBox6.Text + "'," + null + "," + null + "," + null + "," + null + ",'" + TextBox7.Text + "','" + TextBox8.Text + "'," + null + "," + null + "," + null + "," + null + ")";
        }
    }
}